package com.tubeilike.entity;

import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.google.appengine.api.datastore.Text;

@PersistenceCapable
public class Category {

	@PrimaryKey
	@Persistent
	private String alias;
	@Persistent
	private Text title;
	@Persistent
	private Text description;
	@Persistent
	private Text thumbImageUrl;
	@Persistent
	private int count;
	@Persistent
	private int status;
	@Persistent
	private long index;
	
	public long getIndex() {
		return index;
	}

	public void setIndex(long index) {
		this.index = index;
	}

	public String getAlias() {
		return alias;
	}

	public void setAlias(String alias) {
		this.alias = alias;
	}

	public Text getTitle() {
		return title;
	}

	public void setTitle(Text title) {
		this.title = title;
	}

	public Text getDescription() {
		return description;
	}

	public void setDescription(Text description) {
		this.description = description;
	}

	public Text getThumbImageUrl() {
		return thumbImageUrl;
	}

	public void setThumbImageUrl(Text thumbImageUrl) {
		this.thumbImageUrl = thumbImageUrl;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getSubTitle() {
		if (this.title.getValue().length() > 16) {
			return this.title.getValue().substring(0, 16) + "...";
		} else {
			return this.title.getValue();
		}
	}

	public String toString() {
		StringBuilder content = new StringBuilder();
		if (this.alias != null) {
			content.append("<alias>" + this.alias + "</alias>\n");
		}
		if (this.title != null) {
			content.append("<title>" + this.title.getValue() + "</title>\n");
		}
		if (this.thumbImageUrl != null) {
			content.append("<thumbImageUrl>" + this.thumbImageUrl.getValue()
					+ "</thumbImageUrl>\n");
		}
		return content.toString();
	}

	public void transformString(String channelString) {
		Document doc = Jsoup.parse(channelString);
		Elements tmp;
		tmp = doc.select("alias");
		if (tmp != null) {
			this.alias = (tmp.text());
		}
		tmp = doc.select("thumbImageUrl");
		if (tmp != null) {
			this.thumbImageUrl = (new Text(tmp.text()));
		}
		tmp = doc.select("title");
		if (tmp != null) {
			this.title = (new Text(tmp.text()));
		}
	}

}
